package com.hwzn.module.admin.handler;

import static com.hwzn.module.core.enums.ErrorCodeConstants.AUTH_LOGIN_BAD_CREDENTIALS;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExceptionHandlingConfigurer;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.hwzn.module.core.pojo.Result;

/**
 * WebSecurity FailHandler
 *
 * @author jiaobo
 * @date Created in 2024/5/17 10:42
 * @company hw
 **/
@Component
public class WebSecurityFailHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                        AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setContentType(APPLICATION_JSON_VALUE);
        Result commonResult = new Result();
        commonResult.setCode(AUTH_LOGIN_BAD_CREDENTIALS.getCode());
        commonResult.setMsg(AUTH_LOGIN_BAD_CREDENTIALS.getMsg());
        httpServletResponse.getWriter().write(JSON.toJSONString(commonResult));
    }

}
